<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Orgsale extends Backend
{
    
    /**
     * Orgsale模型对象
     * @var \app\admin\model\Orgsale
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Orgsale;
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->assignconfig("org_cate_id", ORG_CATE_ID);
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
//            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $where2 = $this->getSelfDataWhere('orgcate.id', true);
            $filter = $this->request->get("filter", '');
            $filter = (array)json_decode($filter, true);
            $where = '1=1';
            if(isset($filter['name']) && !empty($filter['name'])){
                $where .= ' and orgcate.id = '.$filter['name'];
            }
            if(isset($filter['createtime']) && !empty($filter['createtime'])){
                $filter['createtime'] = str_replace(' - ', ',', $filter['createtime']);
                $arr = array_slice(explode(',', $filter['createtime']), 0, 2);
                if (stripos($filter['createtime'], ',') === false || !array_filter($arr)) {
                  return false;
                }
                if($arr[0] === ''){
                    $where .= ' and c.createtime <= '.strtotime($arr[1]);
                }elseif($arr[1] === ''){
                    $where .= ' and c.createtime >= '.strtotime($arr[0]);
                }else{
                    $where .= ' and c.createtime  BETWEEN  '.strtotime($arr[0]).' AND '.strtotime($arr[1]);
                }
            }
            $data = array();
            $data[0]['id'] = 1;
            $data[0]['type'] = 0;
            $data[0]['total'] = Db::name('consult_reply')->alias('c')
                ->join('edu_third u', 'u.id = c.user_id', 'left')
                ->join('edu_org_cate orgcate', 'u.org_cate_id = orgcate.id', 'left')
                ->where($where)
                ->where($where2)
                ->count();
            $data[0]['total_money'] = 0;
            $data[0]['pay_money'] = 0;
            $data[0]['left_money'] = 0;

            $data[1]['id'] = 2;
            $data[1]['type'] = 1;//课程订单
            $info = Db::name('learn_course_order')->alias('c')
                ->join('edu_learn_course lc', 'lc.id = c.learn_course_id', 'left')
                ->join('edu_org_cate orgcate', 'lc.org_cate_id = orgcate.id', 'left')
                ->where($where)
                ->where($where2)
                ->field('COUNT(c.id) AS total,SUM(CASE WHEN c.price is not null THEN c.price ELSE 0 END) AS total_money,SUM( CASE WHEN c.type= 1 THEN c.price ELSE 0 END) AS pay_money,SUM( CASE WHEN c.type <> 1 THEN c.price ELSE 0 END) AS left_money')
                ->find();
            $data[1]['total'] = isset($info['total'])?$info['total']:0;
            $data[1]['total_money'] =  isset($info['total_money'])?$info['total_money']:0;
            $data[1]['pay_money'] =  isset($info['pay_money'])?$info['pay_money']:0;
            $data[1]['left_money'] =  isset($info['left_money'])?$info['left_money']:0;

            $data[2]['id'] = 3;
            $data[2]['type'] = 2;//测评订单
            $info = Db::name('evaluation_order')->alias('c')
                ->join('edu_third u', 'u.id = c.user_id', 'left')
                ->join('edu_org_cate orgcate', 'u.org_cate_id = orgcate.id', 'left')
                ->where($where)
                ->where($where2)
                ->field('COUNT(c.id) AS total,SUM(CASE WHEN c.price is not null THEN c.price ELSE 0 END) AS total_money,SUM( CASE WHEN c.type= 1 THEN c.price ELSE 0 END) AS pay_money,SUM( CASE WHEN c.type <> 1 THEN c.price ELSE 0 END) AS left_money')
                ->find();
            $data[2]['total'] = isset($info['total'])?$info['total']:0;
            $data[2]['total_money'] =  isset($info['total_money'])?$info['total_money']:0;
            $data[2]['pay_money'] =  isset($info['pay_money'])?$info['pay_money']:0;
            $data[2]['left_money'] =  isset($info['left_money'])?$info['left_money']:0;

            $data[3]['id'] = 4;
            $data[3]['type'] = 3;//测评订单
            $data[3]['total'] =  $data[0]['total'] +  $data[1]['total'] +  $data[2]['total'];
            $data[3]['total_money'] =  $data[0]['total_money'] +  $data[1]['total_money'] +  $data[2]['total_money'];
            $data[3]['pay_money'] =  $data[0]['pay_money'] +  $data[1]['pay_money'] +  $data[2]['pay_money'];
            $data[3]['left_money'] =  $data[0]['left_money'] +  $data[1]['left_money'] +  $data[2]['left_money'];

            $result = array("total" => 4, "rows" => $data , "extend" => ['total' => $data[3]['total'], 'total_money' => $data[3]['total_money'], 'pay_money' =>$data[3]['pay_money'], 'left_money' =>$data[3]['left_money']]);
            return json($result);
        }
        return $this->view->fetch();
    }
}
